<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
        .upLoadFormDadaDiv{
            position: relative;
            cursor: pointer;
        }
        .upLoadFormDadaDiv #AjaxFileData{
            position: absolute;
            top: 0;left: 0;
            opacity: 0;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">转账金额</label>
        <div class="layui-input-block">
            <input type="text" name="investAmount" lay-verify="required|number" lay-reqtext="请输入金额" onkeyup="infCaps(this)" placeholder="请输入金额" value="" class="layui-input">
            <span id="money_span"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">转账商户</label>
        <div class="layui-input-block">
            <input type="text" name="sellerName" disabled value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">服务商</label>
        <div class="layui-input-block">
            <!--<input type="text" name="serviceName" disabled value="" class="layui-input">-->
            <select name="service" lay-filter="serviceName" id="serviceName">
            </select>
        </div>
    </div>
    <input type="hidden" name="images" class="image">
    <div class="layui-form-item">
        <label class="layui-form-label required">转账凭证:</label>

        <div class="layui-upload layui-input-block">
            <div class="upLoadFormDadaDiv layui-btn">
                <i class="layui-icon"></i>上传图片
                <input id="AjaxFileData" type="file" name="file">
            </div>
            <div style="margin: 10px 0">
                <img class="layui-upload-img" id="picShow" src="" height="200" onclick="show_img(this)">
                <p id="demoText"></p>
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注信息</label>
        <div class="layui-input-block">
            <textarea id="commentTxt" name="comment" class="layui-textarea" placeholder="请输入备注信息"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" id='Btn' lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form','upload','tfApi','jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            API = layui.tfApi,
            sellerNameLogin = localStorage.getItem("sellerName"), //window.sessionStorage
            sellerIdLogin = localStorage.getItem("sellerId");

        $("input[name='sellerName']").val(sellerNameLogin);

        API.postAjax('/seller/qryAllService',{},function (res) {
            if(res.code == 0){
                var selData = res.data;
                console.log(selData[0].serviceId);
                console.log(selData[0].serviceName);
                var _h = "<option value='|'>请选择服务商</option>";

                for(var i =0; i<selData.length; i++){
                    _h += "<option value='"+selData[i].serviceId+"|"+selData[i].serviceName+"'>"+selData[i].serviceName+"</option>"
                }
                $("#serviceName").append(_h);
                form.render('select');
            }
        });

        $("#AjaxFileData").change(function(){
        	if (($("#AjaxFileData")[0].files[0].size / 1024) > (2 * 1024)) {
                layer.alert("上传图片不得大于 2M");
                return;
            }
            //$("#picShow").attr("src",URL.createObjectURL($(this)[0].files[0]));
            var file = $('#AjaxFileData').get(0).files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload=function(e){
                //读取成功后返回的一个参数e，整个的一个进度事件
                console.log(e);
                //选择所要显示图片的img，要赋值给img的src就是e中target下result里面
                //的base64编码格式的地址
                $('#picShow').get(0).src = e.target.result;
            }
        });




        //监听提交
        form.on('submit(saveBtn)', function (data) {
        	$('#Btn').addClass("layui-btn-disabled").attr("disabled",true);


            //判断文件类型
            var picFiles = $("#AjaxFileData")[0].files[0];

            var imgType = (picFiles.name.substr(picFiles.name.lastIndexOf("."))).toLowerCase();
            if (imgType != ".jpg" && imgType != ".gif" && imgType != ".jpeg" && imgType != ".png") {
                layer.alert("您上传图片的类型不符合(.jpg|.jpeg|.gif|.png)！");
                $('#Btn').removeClass("layui-btn-disabled").attr("disabled",false);
                return false;
            }
            if (($("#AjaxFileData")[0].files[0].size / 1024) > (5 * 1024)) {
            	$('#Btn').removeClass("layui-btn-disabled").attr("disabled",false);
                layer.alert("上传图片不得大于 5M");
                return;
            }

            var formData = new FormData();
            var _sellerId = sellerIdLogin,
                _serviceId = $("#serviceName").val(),
                _amount = $("input[name='investAmount']").val(),
                _comment = $("#commentTxt").val();

            if(_serviceId==null ||_serviceId=="null" ||_serviceId=="请选择服务商" || _serviceId=="" || _serviceId=="|" ){
                layer.alert("请选择服务商！");
                $('#Btn').removeClass("layui-btn-disabled").attr("disabled",false);
                return;
            }else{
                _serviceId=_serviceId.split("|")[0];
            }
            formData.append("file", picFiles);
            formData.append("sellerAccountId", _sellerId);
            formData.append("serviceId", _serviceId);
            formData.append("investAmount", _amount);
            formData.append("comment", _comment);

            $.ajax({
                url: API.CURL + '/seller/insUpload',
                type: 'post',
                data: formData,
                processData: false,
                contentType: false,
                success: function (res) {
                    var data = JSON.parse(res);
                    console.log("ok=="+data.data);
                    if(data.code == 0){
                        layer.alert('提交申请成功，请等待审核！',function () {
                            layer.close(layer.index);
                            parent.location.reload();
                            $('#Btn').removeClass("layui-btn-disabled").attr("disabled",false);
                        })
                    }
                }
            });

            return false;
        });

     /*   API.postAjax('/seller/qryAllService',{},function (res) {
            if (res.code == 0){
                console.log("data----=="+res.data[0].serviceId);
                $("#serviceName").val(res.data[0].serviceName);
                $("#serviceName").attr("data-id",res.data[0].serviceId);
            }
        });*/

    });

    function show_img(t) {
        var $ = layui.jquery;
        console.log("----->",$)
        //var t = this_(t).find("img");
        //页面层
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['80%', '80%'], //宽高
            shadeClose: true, //开启遮罩关闭
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align:center"><img src="' + $(t).attr('src') + '" /></div>'
        });
    }
    function infCaps(obj) {
        var $ = layui.jquery;
        var money = $(obj).val();
        var fraction = ['角','分'];
        var digit = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'];
        var unit = [['元','万','亿'],['','拾','佰','仟']];
        var head = money < 0?'欠':'';
        money = Math.abs(money);
        var s = '';
        for (var i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(money * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        money = Math.floor(money);
        for (var i = 0; i < unit[0].length && money > 0; i++) {
            var p = '';
            for (var j = 0; j < unit[1].length && money > 0; j++) {
                p = digit[money % 10] + unit[1][j] + p;
                money = Math.floor(money / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
        }
        var sum= head + s.replace(/(零.)*零元/,'元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
        var API = layui.tfApi
       $("#money_span").html("￥"+API.numFun($(obj).val())+"&nbsp;&nbsp;&nbsp;&nbsp;"+sum);
    }
</script>
</body>
</html>